﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using FreightSoft.Models;

namespace FreightSoft.Controllers
{
    public class ShipmentController : Controller
    {
        
        private Entities db = new Entities();
        //
        // GET: /Shipment/

        public ActionResult Index(int? id )
        {
            if (id != null)
            {                
                db.SHIPMENTs.Find(id);
                return View(db.SHIPMENTs.Find(id));
            }
            else
                return View();
        }


        public ActionResult ViewAllShipments()
        {
            var shipments = db.SHIPMENTs.ToList();
            return View(shipments);
        }

        public ActionResult ViewShipments(decimal ?schedule_id)
        {
            var shipments =db.SHIPMENTs.Where(s => s.SCHEDULEID == schedule_id).ToList();
            return View(shipments);
        }

        public void AddShipmentLines(decimal? schedule_id, string route_id, string org_port_id, string dest_port_id)
        {
            var shipments = db.SHIPMENTs.Where(s => s.SCHEDULEID == schedule_id).ToList();
        }

        public ActionResult Create()
        {
            //ViewBag.ORIGINPORT = new SelectList(db.PORTs, "ORIGINPORT", "Port_Name");
            //ViewBag.DESTINATIONPORT = new SelectList(db.PORTs, "DESTINATIONPORT", "Port_Name");

            var port_list = db.PORTs.ToList();
            foreach (var i in port_list)
            {
                i.PORT_ID = i.PORT_ID.Trim();
            }
            var PortList = port_list;
            var dropdownList = new SelectList(PortList, "Port_Id", "Port_Name");
            ViewData["Org_Port_ID"] = dropdownList;
            ViewData["Dest_Port_ID"] = dropdownList;
            return View();
        }

        //
        // POST: /Shipment/Create
        [HttpPost]
        public ActionResult Create(SHIPMENT shipment, FormCollection collection )
        {
            try {
                if (ModelState.IsValid)
                {
                    // call create new method here
                    shipment.ORIGINPORT = collection.Get("selectedOriginPortId");
                    shipment.DESTINATIONPORT = collection.Get("selectedDestPortId");
                    //var idd = db.SHIPMENTs.Count();
                    //var my1 = idd + 1001;

                   // shipment.SHIPMENTID = my1;
                    shipment.STATUS = "Planned";
                    db.SHIPMENTs.Add(shipment);
                    db.SaveChanges();

                    db.Entry(shipment).Reload();
                    var ii = db.SHIPMENTs.Max(p => p.SHIPMENTID);

                    return RedirectToAction("Details", new { Id = ii });
                   // return RedirectToAction("Index");
                }
            }
            catch(System.Data.DataException) 
            { 
                ModelState.AddModelError("", "");
            }
            return View(shipment);
        }

        //
        // GET: /ShipmentSchedule/Details/5

        public ActionResult Details(decimal id)
        {
            SHIPMENT shipment = db.SHIPMENTs.Find(id);
            PORT Org_port = db.PORTs.Find(shipment.ORIGINPORT);
            PORT dest_port = db.PORTs.Find(shipment.DESTINATIONPORT);
            

            shipment.ORIGINPORT = Org_port.PORT_NAME.ToString();
            shipment.DESTINATIONPORT = dest_port.PORT_NAME.ToString();

            if (shipment == null)
            {
                return HttpNotFound();
            }
            return View(shipment);
        }

        public ActionResult CreateSchedule()
        {
            return View();
        }

    }
}
